<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能监控系统</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
            color: #333;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .container {
            width: 90%;
            max-width: 1200px;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        h1 {
            color: #2c3e50;
            margin-bottom: 10px;
        }

        .video-container {
            width: 100%;
            background-color: #000;
            border-radius: 8px;
            overflow: hidden;
            margin-bottom: 30px;
        }

        #video-stream {
            width: 100%;
            height: 500px;
            object-fit: cover;
            border-radius: 8px;
            background-color: #333;
        }

        .notifications-container {
            width: 100%;
        }

        h2 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        #notifications {
            list-style-type: none;
            padding: 0;
            margin: 0;
            max-height: 300px;
            overflow-y: auto;
        }

        .notification-item {
            background-color: #f9f9f9;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 15px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
            transition: transform 0.2s;
        }

        .notification-item:hover {
            transform: translateY(-2px);
        }

        .notification-content {
            display: flex;
            gap: 15px;
        }

        .notification-text {
            flex: 1;
        }

        .notification-image {
            width: 120px;
            height: 120px;
            border-radius: 5px;
            object-fit: cover;
        }

        .notification-time {
            font-size: 0.8rem;
            color: #7f8c8d;
            margin-top: 5px;
        }

        .error-message {
            background-color: #fadbd8;
            color: #e74c3c;
        }

        @media (max-width: 768px) {
            #video-stream {
                height: 300px;
            }

            .notification-content {
                flex-direction: column;
            }

            .notification-image {
                width: 100%;
                height: auto;
                max-width: 150px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>智能监控系统</h1>
            <p>实时监控与智能侦测</p>
        </header>

        <div class="video-container">
            <img src="/video_feed" id="video-stream" alt="实时监控视频流">
        </div>

        <div class="notifications-container">
            <h2>通知中心</h2>
            <ul id="notifications"></ul>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 连接到 WebSocket 服务器
            const socket = io('http://192.168.64.144:5000', {
                reconnection: true,
                reconnectionAttempts: 5,
                reconnectionDelay: 1000
            });

            socket.on('connect', function() {
                console.log('Connected to server');
            });

            socket.on('disconnect', function() {
                console.log('Disconnected from server');
            });

            // 处理通知消息
            socket.on('notification', function(data) {
                const notificationList = document.getElementById('notifications');
                const li = document.createElement('li');
                li.className = 'notification-item';
                
                const notificationHTML = `
                    <div class="notification-content">
                        <div class="notification-text">
                            <strong>${data.message}</strong>
                            <div class="notification-time">${formatTime(new Date())}</div>
                        </div>
                        <img src="${data.image}" class="notification-image">
                    </div>
                `;
                
                li.innerHTML = notificationHTML;
                notificationList.prepend(li); // 添加到列表顶部
                
                // 滚动到最新通知
                notificationList.scrollTop = notificationList.scrollHeight;
                
                // 显示通知提示
                showToast('新通知已到达');
            });

            // 处理连接错误
            socket.onerror = function(event) {
                const notificationList = document.getElementById('notifications');
                const li = document.createElement('li');
                li.className = 'notification-item error-message';
                li.textContent = "无法连接到 WebSocket 服务器，请检查网络连接或服务器状态";
                notificationList.prepend(li);
                
                // 创建错误提示Toast
                const toast = document.createElement('div');
                toast.className = 'error-toast';
                toast.textContent = 'WebSocket连接错误，请检查网络！';
                document.body.appendChild(toast);

                // 尝试重新连接
                setTimeout(() => {
                    socket.connect();
                }, 5000);
            };

            function formatTime(date) {
                return date.toLocaleTimeString();
            }

            function showToast(message) {
                const toast = document.createElement('div');
                toast.style.position = 'fixed';
                toast.style.bottom = '20px';
                toast.style.left = '50%';
                toast.style.transform = 'translateX(-50%)';
                toast.style.background = '#2ecc71';
                toast.style.color = 'white';
                toast.style.padding = '10px 20px';
                toast.style.borderRadius = '4px';
                toast.style.zIndex = '1000';
                toast.textContent = message;
                
                document.body.appendChild(toast);
                
                setTimeout(() => {
                    toast.style.opacity = '0';
                    toast.style.transition = 'opacity 0.5s';
                    setTimeout(() => {
                        document.body.removeChild(toast);
                    }, 500);
                }, 3000);
            }
        });
    </script>
</body>
</html>